package com.lanou.elh.sysmanage.authority_management.service.impl;

import com.google.common.base.Joiner;
import com.lanou.elh.sysmanage.authority_management.entity.Role;
import com.lanou.elh.sysmanage.authority_management.entity.RolePerm;
import com.lanou.elh.sysmanage.authority_management.mapper.RoleMapper;
import com.lanou.elh.sysmanage.authority_management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public void addPerm(Role role, RolePerm rolePerm,Integer[]rolePermid) {
             roleMapper.addRole(role);
        for (int i = 0; i < rolePermid.length; i++) {
            rolePerm.setRoleId(role.getId());
            rolePerm.setPermissionId(rolePermid[i]);
            roleMapper.addRolePerm(rolePerm);
        }
    }

    @Override
    public void deleteRoleById(Integer id) {
        roleMapper.deleteRoleById(id);
    }

    @Override
    public void deletePermById(Integer id) {
        roleMapper.deletePermById(id);
    }

    @Override
    public void deleteInBatchesRoleById(Integer[] id) {
        for (int i = 0; i < id.length; i++) {
            roleMapper.deleteRoleById(id[i]);
        }
    }

    @Override
    public void deleteInBatchesPermById(Integer[] id) {
        for (int i = 0; i < id.length; i++) {
            roleMapper.deletePermById(id[i]);
        }
    }

    @Override
    public void updateRoleById(Role role,RolePerm rolePerm,Integer[]rolePermid) {
          roleMapper.updateRoleById(role);
          roleMapper.deletePermById(role.getId());
        for (int i = 0; i < rolePermid.length; i++) {
            rolePerm.setRoleId(role.getId());
            rolePerm.setPermissionId(rolePermid[i]);
            roleMapper.addRolePerm(rolePerm);
    }}

    @Override
    public List<Role> selectRole() {
        List<Role> roleList = roleMapper.selectRoleList();
        for (int i = 0; i < roleList.size(); i++) {
         List<String>stringList =roleMapper.selectPermissionsByid(roleList.get(i).getId());
            String join = Joiner.on("|").join(stringList);
            roleList.get(i).setPermissions(join);

        }
        return roleList;
    }
}
